package aom.ju.ss.view.main

import aom.ju.ss.R
import aom.ju.ss.databinding.BandPhoneBinding
import aom.ju.ss.mode.LpKey.LOGIN_OK
import aom.ju.ss.mode.LpKey.TOKEN
import aom.ju.ss.mode.WxLoginMode
import aom.ju.ss.utils.*
import aom.ju.ss.utils.http.LP_API.BAND_PHONE
import aom.ju.ss.utils.http.OKHttpUtils
import aom.ju.ss.view.base.BaseActivity
import com.google.gson.Gson

class BandPhoneActivity : BaseActivity() {
    private lateinit var binding: BandPhoneBinding
    private var key: String = ""
    override fun init() {
        binding = viewBinding as BandPhoneBinding
        if (intent.hasExtra("key")){
            key = intent.getStringExtra("key")!!
        }

        binding.send.setOnClickListener {

            sendCode(binding.loginPhone, binding.send)
        }
        binding.loginSubmit.setOnClickListener {
            if (binding.loginPhone.isPhone && binding.verifyEt.isCode) {
                OKHttpUtils(activity)
                    .SetApiUrl(BAND_PHONE)
                    .SetKey("phone", "code", "key")
                    .SetValue(
                        binding.loginPhone.text.toString(),
                        binding.verifyEt.text.toString(),
                        key
                    )
                    .POST(this)
            }
        }
    }

    override fun ok(jsonString: String, httpTY: String) {
        super.ok(jsonString, httpTY)
        when (httpTY) {
            BAND_PHONE -> {

                val mode = Gson().fromJson(jsonString, WxLoginMode::class.java)
                SpUtils.putString(TOKEN, mode.data.token)
                SendMsg.sendEvent(LOGIN_OK)
            }
        }
    }

    override fun error(e: String, jsonString: String, httpTY: String) {
        super.error(e, jsonString, httpTY)
        e.show
    }

    override val lay: Int = R.layout.band_phone
}